package dbsvc

import (
	"errors"
	"os"
	"path/filepath"
	"server/pkg/filehelper"
	"server/pkg/myexcel"
)

// SysCommonDeleteTempDir 删除整个缓存文件
func SysCommonDeleteTempDir() error {
	//把整个缓存都删除了
	baseDir := filepath.Join("static", "temp")
	err := os.RemoveAll(baseDir)
	return err
}

// SaveImportTemplateFile 保存实体的示例文件
func SaveImportTemplateFile(dtoCreate any, entityName string) (string, error) {
	dir := filepath.Join("static", "temp", "import")
	filehelper.CreateDir(dir)
	savePath := filepath.Join(dir, "import_template_"+entityName+".xlsx")
	err := myexcel.WriteStructTagJsonToXlsx(dtoCreate, savePath)
	if err != nil {
		return "", err
	}
	if !filehelper.FileExists(savePath) {
		return "", errors.New("写入模板文件到服务器失败！")
	}

	return savePath, nil
}
